package com.niguang.work.labour.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.niguang.work.labour.api.entity.AppUser;
import com.niguang.work.labour.api.entity.YmylDevice;
import com.niguang.work.labour.api.service.AppUserService;
import com.niguang.work.labour.api.service.IcCardService;
import com.niguang.work.labour.api.service.YmyfFaceService;
import com.niguang.work.labour.api.vo.SynMemberResultVo;
import com.niguang.work.labour.api.vo.SynchroMembersVo;
import com.niguang.work.labour.common.constant.ResultCode;
import com.niguang.work.labour.common.pojo.Result;
import com.niguang.work.labour.common.util.BinSysUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class IcCardServiceImpl implements IcCardService {
    @Resource
    AppUserService appUserService;
    @Resource
    private YmyfFaceService ymyfFaceService;

    /**
     * @param userId
     * @param siteId
     * @param icCard 卡片必须是16进制
     * @return
     */
    @Override
    public Result bindIcCardInfo(Long userId, String siteId, String icCard) {
        QueryWrapper<AppUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq(AppUser.IC_CARD, icCard).eq(AppUser.DEL_FLAG, 0);
        AppUser appUser = appUserService.getOne(userQueryWrapper);
        if (ObjectUtils.isNotEmpty(appUser)) {
            return Result.build(ResultCode.FORBIDDEN, "该卡号已经被绑定");
        }
        YmylDevice ymylDevice = new YmylDevice().selectList(new QueryWrapper<YmylDevice>()
                .lambda()
                .eq(YmylDevice::getSiteId, siteId))
                .parallelStream()
                .findFirst()
                .orElseGet(YmylDevice::new);

        AppUser user = appUserService.getById(userId);
        if (ObjectUtils.isEmpty(user)) {
            return Result.build(ResultCode.FORBIDDEN, "用户不存在!");
        }
        user.setIcCard(icCard);
        if (ymylDevice.getSiteId() == null || StringUtils.isBlank(ymylDevice.getOrgId())) {
            return Result.build(ResultCode.SUCCESS, appUserService.updateById(user));
        }
        SynchroMembersVo.SynchroMembersNoVo synchroMembersNoVo = appUserService.getSynchroMembersByUserId(userId);
        SynchroMembersVo synchroMembersVo1 = new SynchroMembersVo(synchroMembersNoVo.getCommand_id(), ymylDevice.getOrgId(), synchroMembersNoVo.getUserId()
                , synchroMembersNoVo.getFacePhotoUrl(), synchroMembersNoVo.getUserName(), synchroMembersNoVo.getUserBirthday()
                , synchroMembersNoVo.getUserSex(), BinSysUtil.toBinary10(icCard));
        SynMemberResultVo resultVo = ymyfFaceService.synMembers(synchroMembersVo1);

        if (resultVo.getSync_success() == 0) {
            return Result.build(ResultCode.SUCCESS, appUserService.updateById(user));
        }
        return Result.build(ResultCode.FORBIDDEN, resultVo.getSync_fail_desc());

    }

    @Override
    public Result unbindIcCardInfo(String icCard, String siteId, Long userId) {

        AppUser appUser = appUserService.getOne(new QueryWrapper<AppUser>().eq(AppUser.IC_CARD, icCard).eq(AppUser.ID,userId));
        if (appUser == null) {
            return Result.failed("没有该卡号");
        }
        YmylDevice ymylDevice = new YmylDevice().selectList(new QueryWrapper<YmylDevice>()
                .lambda()
                .eq(YmylDevice::getSiteId, siteId))
                .parallelStream()
                .findFirst()
                .orElseGet(YmylDevice::new);
        appUser.setIcCard("");
        if (ymylDevice.getSiteId() == null || StringUtils.isBlank(ymylDevice.getOrgId())) {
            return Result.build(ResultCode.SUCCESS, appUserService.updateIcCardById(appUser.getId()));
        }
        SynchroMembersVo.SynchroMembersNoVo synchroMembersNoVo = appUserService.getSynchroMembersByUserId(userId);
        SynchroMembersVo synchroMembersVo1 = new SynchroMembersVo(synchroMembersNoVo.getCommand_id(), ymylDevice.getOrgId(), synchroMembersNoVo.getUserId()
                , synchroMembersNoVo.getFacePhotoUrl(), synchroMembersNoVo.getUserName(), synchroMembersNoVo.getUserBirthday()
                , synchroMembersNoVo.getUserSex(), null);
        SynMemberResultVo resultVo = ymyfFaceService.synMembers(synchroMembersVo1);

        if (resultVo.getSync_success() == 0) {
            return Result.build(ResultCode.SUCCESS, appUserService.updateIcCardById(appUser.getId()));
        }
        return Result.build(ResultCode.FORBIDDEN, resultVo.getSync_fail_desc());

    }
}
